<?php session_start()?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Quản lý người dùng</title>
<link href="../Twitter_Bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="../CSS/css_admin.css" rel="stylesheet" type="text/css" />
<link rel="stylesheet" type="text/css" href="../CSS/validationEngine.jquery.css"/>

<script type="text/javascript" src="../Twitter_Bootstrap/js/jquery.js"></script>
<script type="text/javascript" src="../Twitter_Bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine.js"></script>
<script type="text/javascript" src="../Scripts/jquery.validationEngine-vi.js"></script>
<script type="text/javascript" src="../Scripts/UserManagementJS.js"></script>


<style type="text/css">
<!--
#tbResult td,#tbResult th
{
	text-align:center;
}
-->
</style>
<script>
		//-----------------------Validation----------------------------------
			$(document).ready(function(){
						
							   $("#formUser").validationEngine();
		//--------------------------Check if Username existed-----------------
		$('#txtUserName').keyup(function(){
										  
								var t=this;
								if(this.value!=this.LastValue)
								{
									if(this.timer)
									{
										clearTimeout(this.timer);
									}
									$('span#check').remove();
									//alert($(this).val());
									if($(this).val()!='')
									{
									$(this).after('<span id="check" style="font-size:10px"><img src="../Images/Icons/ajax-loader.gif" alt=""/>Checking...</span>');
									this.timer = setTimeout(function () {
										$.ajax({
											url: 'process/CheckUserExist.php',
											data: { username: t.value },
											dataType: 'json',
											type: 'get',
											success: function (j) {
												// checking debug
												if(j.ok) {
													$('span#check').remove();
													$('#txtUserName').after(' <span id="check" style="color:red; font-size:10px"> <img src="../Images/Icons/delete.png" alt="" /> '+j.msg+'</span>');
													$('#allow').val('false');
												} else {
													$('span#check').remove();
													$('#txtUserName').after(' <span id="check" style="color:green; font-size:10px"> <img src="../Images/Icons/accept.png" alt="" /> '+j.msg+'</span>');
													$('#allow').val('true');
												}
												/* put the 'msg' field from the $resp array from 
												check_username (php code) in to the validation message */
												//validateUsername.html(j.msg);
											}
										});
									}, 200);
									this.lastValue = this.value;
									}
								}
										 });
		$('#formUser').submit(function(){	
									   if($('#allow').val()=='true')
										   return true;
										return false;
										   });
		//--------------------------------------------------Delete User---------------------------------------
		$('#tbResult a.delete').live('click',function(){
									var check=confirm("Bạn thật sự muốn xóa?");
									var $temp=$(this);
									if(check)
									{
										var id=$(this).parent().parent().children().eq(0).text();
										$.ajax({
											url: 'process/Account_add.php',
											data: { username: id, task: 'delete', deleteduser:'<?php echo $_SESSION["username"]; ?>' },
											dataType: 'json',
											type: 'post',
											error:function()
											{
												alert('error');
											},
											success: function (res) {
												if(res.ok)
												{
														$temp.parent().parent().remove();
													
												}
												alert(res.msg);
											}
										});
									
										
									}
								
																		  });
							   });
		</script>
</head>

<body>
<div class="container" id="Container">
<!--Menu top -->
  <div id="menu">
  	<?php
	include("MainMenu.php");
	include("../Connections/connect_admin.php");
	$page=1;
	$recordPerPage=10;
	$keyword='';
	if(isset($_GET["page"]))
		{
			$page=intval($_GET["page"]);
		}
	$index=($page-1)*$recordPerPage;
	if(isset($_GET["keyword"]))
	{
		$keyword=trim($_GET["keyword"]);
		$query="SELECT u.UserName, u.FullName, u.PhoneNumber, u.MobileNumber, u.Email, u.IsActive FROM `user` u WHERE u.IsDeleted=0 and (u.UserName like '%$keyword%' or u.FullName like '%$keyword%' ) LIMIT $index,$recordPerPage";
	}
	else
	{
		$query="SELECT u.UserName, u.FullName, u.PhoneNumber, u.MobileNumber, u.Email, u.IsActive FROM `user` u WHERE u.IsDeleted=0 LIMIT $index,$recordPerPage";
	}
	?>
    </div>
  </div>
  <div id="divContain">
  <!--Content -->
  <div class="divTitle"><span class="titlePage" >Quản lý người dùng<span></div>
  	<div style="padding-top:30px" >
    <form action="" method="get" id="formSearch" class="form-horizontal">
 		<div class="input-append span7">
        <input type="text" id="txtKeyword" name="keyword" placeholder="Từ khóa..." value="<?php if($keyword !='') echo $keyword; ?>"><button class="btn" type="submit" id="btnSearch"><i class=" icon-search"></i> Tìm kiếm</button>
       
        </div>
         <a type="button" class="btn btn-primary" id="btnAddUser" href="#updateUser" data-toggle="modal"><i class="icon-white icon-plus-sign"></i> Tạo tài khoản mới</a>
    </form>
    <?php
	if(isset($_GET["e"]))
	{
		$error=intval($_GET["e"]);
		switch($error)
		{
			case 0:
	?>
    <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới người dùng THÀNH CÔNG</strong>
    </div>
    <?php
			break;
			case 1:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Thêm mới người dùng THẤT BẠI</strong>
    </div>
     <?php
	 		break;
			case 2:
	?>
     <div class="alert alert-success">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật thông tin người dùng THÀNH CÔNG</strong>
    </div>
     <?php
	 		break;
			case 3:
	?>
     <div class="alert alert-error">
    	    <a class="close" data-dismiss="alert" href="#">&times;</a>
            <strong>Cập nhật thông tin người dùng THẤT BẠI</strong>
    </div>
    <?php
			break;
		}	
		
	}
	?>
    
    <table class="table table-bordered table-striped  " id="tbResult" width="100%" border="0" cellspacing="0" cellpadding="0" >
  <tr>
    <th scope="col">Tên đăng nhập</th>
    <th scope="col">Họ tên</th>
    <th scope="col">Email</th>
    <th scope="col">SĐT Di động</th>
    <th scope="col">SĐT cố định</th>
    <th scope="col">Kích hoạt</th>
    <th scope="col">&nbsp;</th>
    <th scope="col">&nbsp;</th>
   <!-- <th scope="col">&nbsp;</th> -->
  </tr>
  <?php
  $result=mysql_query($query);
  while($row=mysql_fetch_assoc($result))
  {
  ?>
  <tr>
    <td><?php echo $row["UserName"]; ?></td>
    <td><?php echo $row["FullName"]; ?></td>
    <td><?php echo $row["Email"]; ?></td>
    <td><?php echo $row["MobileNumber"]; ?></td>
    <td><?php echo $row["PhoneNumber"]; ?></td>
    <td align="center"><input type="checkbox" name="checkbox" disabled="disabled" <?php if($row["IsActive"]==1) echo "checked='checked'";?> /></td>
    <td><a href="#" title="Xóa" class="delete"><img src="../Images/Icons/delete.png"  alt="delete" /></a></td>
    <td class="<?php  echo $row["UserName"]; ?>"><a href="#updateUser" data-toggle="modal" title="Sửa" class="update"><img src="../Images/Icons/page_edit.png" alt="update" /></a></td>
    <!--<td><a href="#role" data-toggle="modal" title="Phân quyền" class="role"><img src="../Images/Icons/folder_add.png" alt="role" /></a></td> -->
  </tr>
  <?php
  }
  ?>
</table>
	 <div class="pagination pull-right">
      <ul>
        <?php require("process/funcPaging.php");
			
			if(isset($_GET["keyword"])&& $keyword!='')
			{
				$keyword=trim($_GET["keyword"]);
				$query="SELECT u.UserName, u.FullName, u.PhoneNumber, u.MobileNumber, u.Email, u.IsActive FROM `user` u WHERE u.IsDeleted=0 and (u.UserName like '%$keyword%' or u.FullName like '%$keyword%' )";
				$link="Account.php?keyword=".$keyword."&";
			}
			else
			{
				$query="SELECT u.UserName, u.FullName, u.PhoneNumber, u.MobileNumber, u.Email, u.IsActive FROM `user` u WHERE u.IsDeleted=0";
				$link="Account.php?";
			}
			$resAll=mysql_query($query);
			$total=mysql_num_rows($resAll);
			
			Paging($total,$page,5,$recordPerPage,$link);
 		?>
        </ul>
           <!-- <ul>
            <li><a href="#">Prev</a></li>
            <li class="active">
            <a href="#">1</a>
            </li>
            <li><a href="#">2</a></li>
            <li><a href="#">3</a></li>
            <li><a href="#">4</a></li>
            <li><a href="#">Next</a></li>
            </ul>-->
    </div>
	</div>
  <!--footer -->
  </div>
  <div id="footer">
     <?php
	 include("Footer.php");
	 ?>
  </div>
</div>

<!---------------------------Popup Thêm/Cập nhật người dùng----------------------------->
    <div class="modal hide fade"  id="updateUser">
    <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3></h3>
    </div>
    <div class="modal-body">
    <p>
    	<form action="process/Account_add.php" method="post" id="formUser">
        			<input type="hidden" name="task" id="task" value="" />
                    <input type="hidden" id="allow" value="true" />
        	<table width="100%" border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="150px">Tên đăng nhập</td>
                    <td><input name="username" type="text" id="txtUserName" class="validate[required,funcCall[checkSpLetter]]" /></td>
                  </tr>
                  <tr>
                    <td>Họ tên:</td>
                    <td><input name="fullname" type="text" id="txtFullName" class="validate[required]" /></td>
                  </tr>
                  <tr>
                    <td>Email:</td>
                    <td><input name="email" type="text" id="txtEmail" class="validate[custom[email]]" /></td>
                  </tr>
                  <tr>
                    <td>Mật khẩu:</td>
                    <td><input name="password" type="password" id="txtPassword" class="validate[funcCall[checkSpLetter]] validate[equals[txtRePassword]]"  /></td>
                  </tr>
                  <tr>
                    <td>Xác nhận</td>
                    <td><input name="" type="password" id="txtRePassword" class="validate[equals[txtPassword]]" /></td>
                  </tr>
                  <tr>
                    <td>SĐT di động:</td>
                    <td><input name="mobile" type="text" id="txtMobile" /></td>
                  </tr>
                  <tr>
                    <td>SĐT cố định:</td>
                    <td><input name="phone" type="text" id="txtPhone" /></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><label class="checkbox">
                          <input type="checkbox" id="chkActive" name="active" value="active"  />
                          Kích hoạt </label></td>
                  </tr>
                 <tr>
                 	<td></td>
                    <td>
                    <a href="#" class="btn" data-dismiss="modal">Đóng lại</a>
    				<button type="submit" class="btn btn-primary" id="btnUpdateUser">Cập nhật</button>
                    </td>
                 </tr>
              </table>

        </form>
        
        
    </p>
    </div>
    <div class="modal-footer">
    
    </div>
    </div>
    <!---------------------------Popup Phân quyền----------------------------->
     <!--<div class="modal hide fade"  id="role">
    <div class="modal-header">
    <a class="close" data-dismiss="modal">×</a>
    <h3>Phân quyền</h3>
    </div>
    <div class="modal-body">
    <p>
    	<table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td>Người dùng:</td>
                <td><input name="" type="text" id="txtUser" readonly="readonly" /></td>
              </tr>
              <tr>
                <td>Nhóm quyền:<br /><br />
                	<a href="#" class="btn btn-success">Thêm nhóm</a>
                </td>
                <td>
                	    <ul class="nav nav-list">
                        <li class="nav-header">
                        Danh sách nhóm quyền
                        </li>
                        <li value="1">
                        <button type="button" class="delete"><img src="../Images/Icons/delete.png" /></button> Quyền 1
                        </li>
                        <li value=2"">
                         <button type="button" class="delete"><img src="../Images/Icons/delete.png" /></button> Quyền 2
                        </li>
                         <li value="3">
                         <button type="button" class="delete"><img src="../Images/Icons/delete.png" /></button> Quyền 3
                        </li>
                        </ul>
                </td>
              </tr>
        </table>
        
	
    
    </p>
    </div>
    <div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Đóng lại</a>
    </div>
    </div> -->
</body>
</html>
